﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using WP7UIKit.Model;
using WP7UIKit.Helper;

namespace WP7UIKit.ViewModel
{
    public class WaterFallViewModel : ViewModelBase
    {
        #region Fields
        private ObservableCollection<DataItem> _items;
        private int _columnCount = 3;
        private double _columnMargin = 3;
        private double _width;
        private DataItem _selectedItem;
        private bool _isInDelMode;
        #endregion

        #region Constructor
        public WaterFallViewModel()
        {
            _items = new ObservableCollection<DataItem>()
            {
                new DataItem("http://img.geilicdn.com/taobao18645452669_146x217.jpg",Width,1.49),
                new DataItem("http://img.geilicdn.com/taobao15288894227_146x113.jpg",Width,0.78),
                new DataItem("http://img.geilicdn.com/taobao16233163498_146x216.jpg",Width,1.48),
                new DataItem("http://img.geilicdn.com/taobao18607076827_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao18645576996_146x200.jpg",Width,1.37),
                new DataItem("http://img.geilicdn.com/taobao16361967323_146x239.jpg",Width,1.64),
                new DataItem("http://img.geilicdn.com/taobao15316137318_146x239.jpg",Width,1.64),
                new DataItem("http://img.geilicdn.com/taobao18352264592_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao15326973334_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao18628668995_146x163.jpg",Width,1.12),
                new DataItem("http://img.geilicdn.com/taobao15310286319_146x194.jpg",Width,1.33),
                new DataItem("http://img.geilicdn.com/taobao15325033888_146x219.jpg",Width,1.50),
                new DataItem("http://img.geilicdn.com/taobao18650444558_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao15313630567_146x205.jpg",Width,1.41),
                new DataItem("http://img.geilicdn.com/taobao14403594976_146x162.jpg",Width,1.11),
                new DataItem("http://img.geilicdn.com/taobao18640596407_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao15310286390_146x219.jpg",Width,1.50),
                new DataItem("http://img.geilicdn.com/taobao18640676736_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao18567548031_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao18656684068_146x148.jpg",Width,1.02),
                new DataItem("http://img.geilicdn.com/taobao15311430726_146x219.jpg",Width,1.50),
                new DataItem("http://img.geilicdn.com/taobao15295809618_146x219.jpg",Width,1.50),
                new DataItem("http://img.geilicdn.com/taobao16334799642_146x219.jpg",Width,1.50),
                new DataItem("http://img.geilicdn.com/taobao18649444754_146x176.jpg",Width,1.21),
                new DataItem("http://img.geilicdn.com/taobao16375767585_146x194.jpg",Width,1.33),
                new DataItem("http://img.geilicdn.com/taobao14866223387_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao18660484189_146x201.jpg",Width,1.38),
                new DataItem("http://img.geilicdn.com/taobao15305274194_146x219.jpg",Width,1.50),
                new DataItem("http://img.geilicdn.com/taobao18606508543_146x144.jpg",Width,0.99),
                new DataItem("http://img.geilicdn.com/taobao15318501915_146x96.jpg",Width,0.66),
                new DataItem("http://img.geilicdn.com/taobao15317150865_146x147.jpg",Width,1.01),
                new DataItem("http://img.geilicdn.com/taobao15318693452_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao15318649740_146x148.jpg",Width,1.02),
                new DataItem("http://img.geilicdn.com/taobao16362611554_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao16320219744_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao18632168603_146x194.jpg",Width,1.33)
            };
            this.OnItemSelectCommand = new RelayCommand(ItemSelectExecute);
            this.DeleteItemCommand = new RelayCommand(DeleteItemExecute);
            this.LoadDataCommand = new RelayCommand(LoadDataExecute);
        }
        #endregion

        #region Command
        public RelayCommand OnItemSelectCommand { get; private set; }
        public RelayCommand DeleteItemCommand { get; private set; }
        public RelayCommand LoadDataCommand { get; private set; }
        #endregion

        #region Properties
        public ObservableCollection<DataItem> Items
        {
            get { return _items; }
            set
            {
                if (value != _items)
                {
                    _items = value;
                    this.RaisePropertyChanged(() => this.Items);
                }
            }
        }

        public int ColumnCount
        {
            get { return _columnCount; }
            set
            {
                if (value != _columnCount)
                {
                    _columnCount = value;
                    this.RaisePropertyChanged(() => this.ColumnCount);
                }
            }
        }

        public double ColumnMargin
        {
            get { return _columnMargin; }
            set
            {
                if (value != _columnMargin)
                {
                    _columnMargin = value;
                    this.RaisePropertyChanged(() => this.ColumnMargin);
                }
            }
        }

        public double Width
        {
            get
            {
                //480 可以自己来设定
                _width = (480 - ColumnMargin * (ColumnCount + 1)) / ColumnCount;
                return _width;
            }
        }

        public DataItem SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                if (value != _selectedItem)
                {
                    _selectedItem = value;
                    this.RaisePropertyChanged(() => this.SelectedItem);
                }
            }
        }

        public bool IsInDelMode
        {
            get { return _isInDelMode; }
            set
            {
                if (value != _isInDelMode)
                {
                    _isInDelMode = value;
                    this.RaisePropertyChanged(() => this.IsInDelMode);
                }
            }
        }
        #endregion

        #region Method
        private void ItemSelectExecute()
        {
            SelectedItem.IsChecked = !SelectedItem.IsChecked;
            SelectedItem = null;
        }

        private void DeleteItemExecute()
        {
            this.IsInDelMode = true;
            for (int i = _items.Count - 1; i >= 0; i--)
            {
                DataItem item = _items.ElementAt(i);
                if (item.IsChecked)
                {
                    _items.RemoveAt(i);
                }
            }
            this.IsInDelMode = false;
        }

        private void LoadDataExecute()
        {
            List<DataItem> dataItemList = new List<DataItem>()
            {
                new DataItem("http://img.geilicdn.com/taobao18645452669_146x217.jpg",Width,1.49),
                new DataItem("http://img.geilicdn.com/taobao15288894227_146x113.jpg",Width,0.78),
                new DataItem("http://img.geilicdn.com/taobao16233163498_146x216.jpg",Width,1.48),
                new DataItem("http://img.geilicdn.com/taobao18607076827_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao18645576996_146x200.jpg",Width,1.37),
                new DataItem("http://img.geilicdn.com/taobao16361967323_146x239.jpg",Width,1.64),
                new DataItem("http://img.geilicdn.com/taobao15316137318_146x239.jpg",Width,1.64),
                new DataItem("http://img.geilicdn.com/taobao18352264592_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao15326973334_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao18628668995_146x163.jpg",Width,1.12),
                new DataItem("http://img.geilicdn.com/taobao15310286319_146x194.jpg",Width,1.33),
                new DataItem("http://img.geilicdn.com/taobao15325033888_146x219.jpg",Width,1.50),
                new DataItem("http://img.geilicdn.com/taobao18650444558_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao15313630567_146x205.jpg",Width,1.41),
                new DataItem("http://img.geilicdn.com/taobao14403594976_146x162.jpg",Width,1.11),
                new DataItem("http://img.geilicdn.com/taobao18640596407_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao15310286390_146x219.jpg",Width,1.50),
                new DataItem("http://img.geilicdn.com/taobao18640676736_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao18567548031_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao18656684068_146x148.jpg",Width,1.02),
                new DataItem("http://img.geilicdn.com/taobao15311430726_146x219.jpg",Width,1.50),
                new DataItem("http://img.geilicdn.com/taobao15295809618_146x219.jpg",Width,1.50),
                new DataItem("http://img.geilicdn.com/taobao16334799642_146x219.jpg",Width,1.50),
                new DataItem("http://img.geilicdn.com/taobao18649444754_146x176.jpg",Width,1.21),
                new DataItem("http://img.geilicdn.com/taobao16375767585_146x194.jpg",Width,1.33),
                new DataItem("http://img.geilicdn.com/taobao14866223387_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao18660484189_146x201.jpg",Width,1.38),
                new DataItem("http://img.geilicdn.com/taobao15305274194_146x219.jpg",Width,1.50),
                new DataItem("http://img.geilicdn.com/taobao18606508543_146x144.jpg",Width,0.99),
                new DataItem("http://img.geilicdn.com/taobao15318501915_146x96.jpg",Width,0.66),
                new DataItem("http://img.geilicdn.com/taobao15317150865_146x147.jpg",Width,1.01),
                new DataItem("http://img.geilicdn.com/taobao15318693452_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao15318649740_146x148.jpg",Width,1.02),
                new DataItem("http://img.geilicdn.com/taobao16362611554_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao16320219744_146x146.jpg",Width,1.00),
                new DataItem("http://img.geilicdn.com/taobao18632168603_146x194.jpg",Width,1.33)
            };
            dataItemList.ToObservableCollection<DataItem>(Items);
        }
        #endregion
    }
}
